home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10582 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: noc.tor.hookup.net!news
  2. From: Richard Steadman <rsteadma@micromedia.on.ca>
  3. Newsgroups: comp.lang.c
  4. Subject: Q: Pointer to an allocated array of structures
  5. Date: Mon, 18 Mar 1996 15:44:24 -0500
  6. Organization: Micromedia
  7. Message-ID: <314DCB28.4385@micromedia.on.ca>
  8. NNTP-Posting-Host: keeper.mmltd.com
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (Win95; I)
  13.  
  14. Hello. I have a bit of code which runs ok, but lint (lclint)
  15. complains about it. I was wondering if I was using pointers
  16. the correct way, or if I was stretching the rules a bit.
  17.  
  18. A structure is typedef'ed as follows:
  19.  
  20.     typedef struct {
  21.         long a;
  22.         long b;
  23.         } S;
  24.  
  25. Then I declare a pointer to this type:
  26.  
  27.     S *pointer;
  28.  
  29. and use malloc() to get space for it:
  30.  
  31.     pointer=malloc(1000 * sizeof(S));
  32.  
  33. Then I can reference the array the usual way:
  34.  
  35.     pointer[index].a = some_long;
  36.  
  37. This seems to run ok, but produces various error messages from
  38. lclint when I'm using the array:
  39.  
  40. >program.c:87,8: Passed storage *pointer contains 2 undefined fields: 
  41. >a, b.  Storage derivable from a parameter, return value or
  42. >global is not defined. Use /*@out@*/ to denote passed or returned
  43. >storage which need not be defined. Use -compdef to suppress message.    [when passing "pointer" to a function: func(pointer);]
  44.  
  45. >program.c:90,13: Field pointer[].b used before definition
  46. >An rvalue is used that may not be initialized to a value on some 
  47. >execution path.  Use -usedef to suppress message.    [when using a variable as the array index: index[i].a=var; ]
  48.  
  49. >program.c:113,26: Unqualified storage passed as only: realloc 
  50. >(pointer,...).  Unqualified storage is transferred in an inconsistent
  51. >way.  Use  -unqualifiedtrans to suppress message.
  52. >   cvrsort.c:63,8: Storage index becomes unqualified    [when reallocating]
  53.  
  54. I have also tried declaring the pointer as:
  55.     S (*pointer)[];
  56.  
  57. but this doesn't seem to help. I don't know if one of these
  58. pointer declarations is wrong, or whether one is preferable.
  59. The first method seems to work at least, though there seems
  60. to be some problems when passing the pointer to a function
  61. which uses it as an array.
  62.  
  63. Basically, I just want to know the right way to declare an
  64. array of structures which will use allocated space, without
  65. using pointers to the individual structures (no arrays of
  66. pointers).
  67.  
  68. Thanks.
  69.  
  70. Richard Steadman
  71. rsteadma@mmltd.com
  72.